<?php
// +----------------------------------------------------------------------
// | ProjectName : domall
// +----------------------------------------------------------------------
// | Description :  配置管理
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.idowe.com All rights reserved.
// +----------------------------------------------------------------------
// | Authors : Doogie <461960962@qq.com>  Date : 2016-03-16
// +----------------------------------------------------------------------

namespace app\admin\controller;

class Setting extends Base{
    private static $setting_m;
    public function __construct() {
        parent::__construct();
        self::$setting_m = D('Setting');
    }

    public function index(){
        $group = I('get.group', 1);
        $group_list = self::$setting_m->getByName('config_group_list');
        $group_list = parse($group_list['type'], $group_list['value']);
        $map = ['group' => $group];
        $list = self::$setting_m->page(I('get.'.C('var_page')), C('admin_page_rows'))->where($map)->order('sort asc')->select();
        $page = $this->page('Setting', $map);

        $this->assign([
            'list' => $list,
            'group_list' => $group_list,
            'group' => $group,
            'page' => $page->show()
        ]);
        return $this->display();
    }

    /**
     * 添加配置
     * @return mixed|string
     */
    public function add(){
        if(IS_POST){
            $return_data = ['code' => 1, 'url' => '', 'msg' => '添加成功'];
            $result = self::$setting_m->validate([
                // name、title和value是需要验证的字段名
                'name'=> ['\w{2,32}','配置标识不符合规范'],
                'title' => ['require','配置标题不符合规范'],
                'value' => ['require','配置值不符合规范'],
            ])->auto([
                // status和create_time是需要填充的字段名
                'status'=> 1, //
                'create_time' => NOW_TIME,
            ])->create();

            if(!$result){
                // 验证失败 返回错误信息
                $return_data['msg'] = self::$setting_m->getError();
                $return_data['code'] = 0;
            }else{
                self::$setting_m->add();
            }
            echo json_encode($return_data);exit;
        }else{

            $this->assign([
                'group_list' => self::$setting_m->groups(),
                'type_list' => self::$setting_m->types(),
                'meta_title' => '新增配置',
                'post_url' => url('add')
            ]);

            return $this->display();
        }
    }

    /**
     * 添加配置
     * @return mixed|string
     */
    public function edit($id){
        if(IS_POST){
            $return_data = ['code' => 1, 'url' => '', 'msg' => '修改成功'];
            $result = self::$setting_m->validate([
                // name、title和value是需要验证的字段名
                'name'=> ['\w{2,32}','配置标识不符合规范'],
                'title' => ['require','配置标题不符合规范'],
                'value' => ['require','配置值不符合规范'],
            ])->auto([
                'update_time' => NOW_TIME,
            ])->create();

            if(!$result){
                // 验证失败 返回错误信息
                $return_data['msg'] = self::$setting_m->getError();
                $return_data['code'] = 0;
            }else{
                self::$setting_m->save();
            }
            echo json_encode($return_data);exit;
        }else{

            $this->assign([
                'info' => self::$setting_m->find($id),
                'group_list' => self::$setting_m->groups(),
                'type_list' => self::$setting_m->types(),
                'meta_title' => '修改配置',
                'post_url' => url('edit')
            ]);

            return $this->display('add');
        }
    }
}